package com.kdtech.analyse.outside;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.AnalyseNews;
import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;

import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.StringUtils;

/**
 * 经济日报
 * @author Persh
 *
 */
public class HketNewsAnalyse implements AnalyseNews {

	private static final String[] regex={
		"http://www.hket.com/eti/article/.*",
		"http://www.hket.com/eti/inews/content/[0-9]*",
		"http://topick.hket.com/article/[0-9]*/.*",
		"http://china.hket.com/article/[0-9]*/.*",
	};


	
	public boolean isDetailPage(String url) {
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return false;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();

		news.setUrl(url);
		String title=null;
		String content=null;
		String author=null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);
		title =doc.select("title").text();
		if(title.contains("-")) title=StringUtils.substringBefore(title, "-");
		content=HtmlCleaner.getContentHtml(url,doc.select("div#eti-article-content"));
		if(StringUtils.isBlank(content)){
			content=HtmlCleaner.getContentHtml(url,doc.select("div.content p"));
		}
		if(StringUtils.isBlank(content)){
			content=HtmlCleaner.getContentHtml(url,doc.select("#content-main"));
		}
		author=JSoupUtils.matchAuthor(doc, "撰文:");
		date=DateUtils.matchDate(doc.select("div#eti-article-functions").text());
		if(date==null){
			date=DateUtils.matchDate(doc.select(".date").text());
		}
		if(date==null){
			date=DateUtils.matchDate(doc.select("div[style*=font-weight:bold; color:#000; margin:25px 0]").text());
		}

		if(StringUtils.isBlank(title)){
		}
		news.setTitle(StringUtils.trimSpace(title));
		news.setContent(content);
		news.setAuthor(author);
		news.setDate(date);

		return news;
	}

	

	

	
}
